package cn.itsource.param.service.impl;

import cn.itsource.param.domain.Param;
import cn.itsource.param.mapper.ParamMapper;
import cn.itsource.param.service.ParamService;
import cn.itsource.system.query.ParamQuery;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ParamServiceImpl implements ParamService {

    @Autowired
    private ParamMapper paramMapper;


    @Override
    public Map<String, Object> param() {
       List<Param> paramList = paramMapper.selectAll();
       return paramList.stream().collect(Collectors.toMap
               (Param::getParamKey,Param::getParamValue));
    }

    /**
     * 分页，查询
     * @param paramQuery
     * @return
     */
    @Override
    public PageInfo<Param> pageInfo(ParamQuery paramQuery) {
        PageHelper.startPage(paramQuery.getCurrentPage(),paramQuery.getPageSize());
        List<Param> params = paramMapper.selectAllPage(paramQuery);
        return new PageInfo<Param>(params);
    }

    /**
     * 新增和修改
     * @param p
     */
    @Override
    public void save(Param p) {
        if (p.getId()==null){
            paramMapper.insert(p);
        }else {
            paramMapper.updateByPrimaryKeySelective(p);
        }
    }

    @Override
    public void deleteAll(List<Param> p) {
        paramMapper.deleteAll(p);
    }

    @Override
    public void deleteByPrimaryKey(Long id) {
        paramMapper.deleteByPrimaryKey(id);
    }
}
